<?php
$startPic="";
$endPic="";
$uploadDir = 'upload/images';
$upload_ret = 1;
$uris ;
$startTargetFile;
$endTargetFile;
$server_path = "http://118.178.94.215/";


 //获取上传的图片
if( isset($_FILES['startFile'])){
    $startPic = $_FILES['startFile']['tmp_name'];
}
if(isset($_FILES['endFile'])){
    $endPic = $_FILES['endFile']['tmp_name'];
}

if($startPic != ""){
    // 创建文件夹
    if(!file_exists($uploadDir)){
        mkdir($uploadDir, 0777);
    }
    // 用时间戳来保存图片，防止重复
    $startTargetFile = $uploadDir . '/' . time() . '.'.get_extname($_FILES['startFile']['name']);
    if( !move_uploaded_file($startPic, $startTargetFile)){
        $upload_ret = 0 ;
    }

    $startTargetFile = $server_path . $startTargetFile;
} else {
    $startTargetFile = "";
}

if($endPic != ""){
    // 创建文件夹
    if(!file_exists($uploadDir)){
        mkdir($uploadDir, 0777);
    }
    // 用时间戳来保存图片，防止重复
    $endTargetFile = $uploadDir . '/' . time() . '.'.get_extname($_FILES['endFile']['name']);
    if( !move_uploaded_file($endPic, $endTargetFile)){
        $upload_ret = 0;
    }
    $endTargetFile = $server_path . $endTargetFile;
} else {
    $endTargetFile = "";
}

if($upload_ret == 1){
    $uris['startImageUri'] = $startTargetFile;
    $uris['endImageUri'] = $endTargetFile;
    echo json_encode($uris);
} else {
    echo $upload_ret;
}


// 获得扩展名函数
function get_extname($fname) {
    $temp = explode(".", $fname);
    $where = count($temp) - 1;
    return $temp[$where];
}



//$action = $_GET['act'];
//if ($action == 'delimg') {
//    $filename = $_POST['imagename'];
//    $delPath = $_SERVER['DOCUMENT_ROOT'] . $filename;
//    if (!empty($delPath)) {
//        unlink($delPath);
//        echo '1';
//    } else {
//        echo '删除失败.';
//    }
//} else {
//    $picname = $_FILES['mypic']['name'];
//    $picsize = $_FILES['mypic']['size'];
//    if ($picname != "") {
//        if ($picsize > 1024000) {
//            echo '图片大小不能超过1M';
//            exit;
//        }
//        $type = strstr($picname, '.');
//        $type = strtolower($type);
//        if ($type != ".gif" && $type != ".jpg" && $type != ".png") {
//            echo '图片格式不对！';
//            exit;
//        }
//        //上传路径
//        $rootPath = $_SERVER['DOCUMENT_ROOT'];
//        list($usec, $sec) = explode(".", microtime());
//        $haoMiao = (float) $sec; //毫秒
//        $target_path = '/img/test/' . date("/Y/m/d/His_") . $haoMiao . "_orgtemp." . get_extname($_FILES['mypic']['name']);
//        creDir($rootPath . $target_path);
//        if (!empty($_FILES['mypic']['tmp_name'])) {
//            move_uploaded_file($_FILES['mypic']['tmp_name'], $rootPath . $target_path);
//        } else {
//            $target_path = '';
//            msg("上传图片失败！");
//        }
//    }
//    $size = round($picsize / 1024, 2);
//    $arr = array(
//        'name' => $picname,
//        'pic' => $target_path, //$pics,
//        'size' => $size
//    );
//    echo json_encode($arr);
//}
//


////创建一个新的文件夹
//function creDir($newPath) {
//    $temp = explode('/', $newPath);
//    $p = '';
//    $result = true;
//
//    $len = count($temp);
//    if (!(strpos($temp[$len - 1], '.') === false)) {
//        array_pop($temp);
//    }
//    foreach ($temp as $value) {
//        $p.=$value . '/';
//        if (!is_dir($p))
//            $result = $result && @mkdir($p);
//    }
//    return $result;
//}

?>